default_platform(:android)

platform :android do
  desc "Deploy a new version to the Google Play Internal track"
  lane :internal do
    # Clean
    sh("flutter", "clean")
    
    # Pub get
    sh("flutter", "pub", "get")
    
    # Bumps Version Code
    android_set_version_code()

    # Build the app
    sh("flutter", "build", "appbundle", "--dart-define=ENV=PROD")

    # Gets version info
    version_info = flutter_version(
      pubspec_location: "../pubspec.yaml",
    )

    # Upload the app to Play console
    supply(
      track: "internal",
      json_key_data: ENV["GOOGLE_SERVICE_ACCOUNT"],
      skip_upload_images: true,
      skip_upload_screenshots: true,
      skip_upload_apk: true,
      skip_upload_metadata: true,
      version_name: version_info["version_name"],
      version_code: android_get_version_code(),
      aab: "../build/app/outputs/bundle/release/app-release.aab",
    )
  end

  # Release a new version to Firebase Distribution
  #
  # It assumes that the app version is correctly set.
  desc "Release app to Firebase Distribution"
  lane :firebase do
    # # Clean
    # sh("flutter", "clean")

    # # Pub get
    # sh("flutter", "pub", "get")

    # # Build the app
    # sh("flutter", "build", "appbundle", "--dart-define=ENV=PROD")

    firebase_app_distribution(
      android_artifact_path: '../build/app/outputs/bundle/release/app-release.aab',
      android_artifact_type: 'AAB',
      app: ENV["MEMO_ANDROID_FIREBASE_APP_ID"],
      service_credentials_file: "./app/google-services.json",
      groups: "internal-testers",
      service_credentials_file: "../firebase_distribution_service_account.json"
    )
  end
end
